package controller.expertInfo.expertEdit;

import StaticFinal.CrudType;
import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.BaseController;
import controller.common.StaticConstant;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.dictionary.CertificateTypeServices;
import services.dictionary.MajorTypeServices;
import services.expertInfo.ExpertMajorServices;
import wt.entity.dictionary.MajorType;
import wt.entity.expertInfo.ExpertMajor;
import wt.webPlugin.ZTree;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: expert
 */
@Controller
@RequestMapping("expertInfo/expertEdit/expertMajor")
public class ExpertSelfMajorController extends BaseController {

    @Autowired
    ExpertMajorServices expertMajorServices;
    @Autowired
    CertificateTypeServices certificateTypeServices;
    @Autowired
    MajorTypeServices majorTypeServices;

    @RequiresPermissions("expertInfo:expertEdit:default")
    @RequestMapping("index")
    public String index(Map map) throws Exception {
        boolean isEdit = false;
        List<ExpertMajor> expertMajorList = expertMajorServices.expertSelectByExpertId(getLoginAccount().getExpertBase().getId());

        if (expertMajorList != null && !expertMajorList.isEmpty()) {

            List<String> majorTypeIdList = new ArrayList<String>();

            for (ExpertMajor expertMajor : expertMajorList) {
                majorTypeIdList.add(expertMajor.getMajorId());
            }

            List<MajorType> majorTypeList = majorTypeServices.selectTreeByLeafId(majorTypeIdList);
            JSONArray jsonArray = new JSONArray();
            for (MajorType majorType : majorTypeList) {
              JSONObject jsonObject= ZTree.createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getName());
                for (ExpertMajor expertMajor : expertMajorList) {
                        if (expertMajor.getCrudType() != CrudType.select&&expertMajor.getMajorId().equals(majorType.getId())) {
                            isEdit = true;
                            if (expertMajor.getCrudType() == CrudType.insert) {
                                 jsonObject.put(ZTree.ICON_SKIN,"add");
                            } else {
                                jsonObject.put(ZTree.ICON_SKIN,"remove");
                            }
                        }
                }
                jsonArray.add(jsonObject);
            }


//            for (ExpertMajor expertMajor : expertMajorList) {
//                if (expertMajor.getCrudType() != CrudType.select) {
//                    isEdit=true;
//                    for (MajorType majorType : majorTypeList) {
//                        if (expertMajor.getMajorId().equals(majorType.getId())) {
//                            if (expertMajor.getCrudType()==CrudType.insert){
//                                majorType.setName("新增 "+majorType.getName());
//                            }else{
//                                majorType.setName("删除 "+majorType.getName());
//                            }
//                        }
//                    }
//                }
//            }

//
//            if (majorTypeList != null) {
//                for (MajorType majorType : majorTypeList) {
//                    if (checkedByLive && majorType.getLive() == 1) {
//                    } else {
//                        jsonArray.add(createZTreeJSONObject(majorType.getId(), majorType.getPid(), majorType.getName()));
//                    }
//                }
//            }
//
//
//            JSONArray jsonArray = ZTree.majorType2ZTreeJSONArray(majorTypeList, false);
            map.put("data", jsonArray);
        } else {
            map.put("data", "[]");
        }
        map.put("isEdit", isEdit);
        return "expertInfo/expertEdit/expertMajor/index";
    }

    @RequiresPermissions("expertInfo:expertEdit:default")
    @RequestMapping("editDlg")
    public String editDlg(Map map) throws Exception {

        List<MajorType> majorTypeList = majorTypeServices.selectMajorType(null, true);
        List<ExpertMajor> expertMajorList = expertMajorServices.expertSelectByExpertId(getLoginAccount().getExpertBase().getId());

        for (MajorType majorType : majorTypeList) {
            majorType.setLive((short) 0);
            for (ExpertMajor expertMajor : expertMajorList) {
                if (expertMajor.getMajorId().equals(majorType.getId())) {
                    majorType.setLive((short) 1);
                    break;
                }
            }
        }

        map.put(StaticConstant.EDIT_DATA, ZTree.majorType2ZTreeJSONArray(majorTypeList, true));
        map.put(StaticConstant.EDIT_URL, "expertInfo/expertEdit/expertMajor/update");
        map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);
        return "expertInfo/expertEdit/expertMajor/editDlg";
    }

    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("update")
    public synchronized CommonResult update(
            @RequestParam(value = "insertLeafIds[]", required = false) List<String> insertLeafIdList,
            @RequestParam(value = "deleteLeafIds[]", required = false) List<String> deleteLeafIdList) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        expertMajorServices.expertUpdateMajor(insertLeafIdList, deleteLeafIdList, getLoginAccount().getExpertBase().getId());
        return commonResult.setSuccess();
    }
    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("cancelApply")
    public CommonResult cancelApply(@RequestParam(value = "expertId", required = false) String expertId) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        if (StringUtils.isBlank(expertId)) {
            expertId = getLoginAccount().getExpertBase().getId();
        }
        expertMajorServices.clearUnauditedRecord(expertId);
        return commonResult.setSuccess();
    }

}
